﻿using Struct;
using System;
using System.Runtime.InteropServices;

namespace FastCopyWin
{
    public class Win32Api
    {
        // 处理事件委托.
        public delegate int HookHandle(int nCode, int wParam, IntPtr lParam);
        // 设置钩子
        [DllImport("user32.dll")]
        public static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId);

        // 调用下一个钩子
        [DllImport("user32.dll")]
        public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        // 获取一个应用程序或动态链接库的模块句柄 
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);

        // 获取鼠标坐标
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out POINT lpPoint);

        // 获取屏幕缩放参数
        [DllImport("gdi32.dll")]
        public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

        // 获取屏幕
        [DllImport("User32.dll", EntryPoint = "GetDC")]
        public static extern IntPtr GetDC(IntPtr hWnd);

        // 释放屏幕
        [DllImport("User32.dll", EntryPoint = "ReleaseDC")]
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        // 获取分辨率
        [DllImport("User32.dll")]
        public static extern int GetSystemMetrics(int hWnd);

        // 设置剪切板监听
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);

        // 发送消息 进行消息传递
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        // 获取最顶层的窗口
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        // 获取窗口线程 ID
        [DllImport("user32.dll")]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

        // 将一个线程的输入处理机制附加到或分离另一个线程的输入处理机制
        [DllImport("user32.dll")]
        public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

        // 获取窗口线程信息
        [DllImport("user32.dll")]
        public static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);

        // 模拟外设输入 比如鼠标、键盘
        [DllImport("user32.dll", SetLastError = true)]
        public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

        // 激活指定窗口
        [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
        
        // 设置为活动窗口
        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        // 使指定窗口获取焦点
        [DllImport("user32.dll")]
        public static extern IntPtr SetFocus(IntPtr hWnd);

        public static GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd)
        {
            if (hwnd != IntPtr.Zero)
            {
                uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
                GUITHREADINFO guiThreadInfo = new GUITHREADINFO();
                guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo);
                if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false)
                    return null;
                return guiThreadInfo;
            }
            return null;
        }

    }
}
